package hn.sigit.logic.bpm;


import hn.sigit.model.hnd.administrative.HND_RequestMetaData;
import hn.sigit.model.hnd.administrative.HND_RequestSubType;
import hn.sigit.model.hnd.administrative.HND_RequestType;
import hn.sigit.model.hnd.administrative.dao.HND_RequestMetaDataDAO;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Locale;

import javax.faces.model.SelectItem;

import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.Seam;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.bpm.Transition;
import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;

@Name("transactionController")
@Scope(value=ScopeType.CONVERSATION)
@AutoCreate
public class TransactionController implements Serializable {
	private static final long serialVersionUID = 5342502595494847707L;
	
	private TransactionProcessBean transactionProcess;
	
	//Variables for PRESENTATION activity--------------------------------
	private Date presentationDate;
	private Long partyId;
	private String partyNationalIdentity;
	private String partyName;
	private String cashiersCode;
	private BigDecimal payment;
	private String description;
	private List<AttachedFileData> attachedFiles;
	private String receptionistUserName;
	private String receptionistFullName;
	private Long presentationId;
	//END Variables for PRESENTATION activity----------------------------
	
	//Variables for ANALYSIS activity------------------------------------
	private HND_RequestType requestType;
	private HND_RequestSubType requestSubType;
	private List<ParcelData> parcelDataList;
	private String analysisDictum;
	private String additionalObservations;
	private boolean requestToProceed;
	private String analystUserName;
	private String analystFullName;
	//END Variables for ANALYSIS activity--------------------------------
	
	//Variables for DATAENTRY activity-----------------------------------
	private String dataEntryUserName;
	private String dataEntryFullName;
	//END Variables for DATAENTRY activity-------------------------------
	
	//Variables for APPROVAL activity------------------------------------
	private String approverUserName;
	private String approverFullName;
	//END Variables for APPROVAL activity--------------------------------

	private String gid;
	private boolean attachInfo = true;
	
	private String editMode = "0"; //0: agregando, 1: editando
	private AttachedFileData selectedFile;
	private ParcelData selectedParcelData;
	private int currentRow;
	private String propertyCode;
	

	public TransactionController() {
		transactionProcess = (TransactionProcessBean)Component.getInstance("transactionProcess");
		if (transactionProcess != null) {
			setPresentationDate(transactionProcess.getPresentationDate());
			setReceptionistUserName(transactionProcess.getReceptionistUserName());
			setReceptionistFullName(transactionProcess.getReceptionistFullName());
			setPartyId(transactionProcess.getPartyId());
			setPartyNationalIdentity(transactionProcess.getPartyNationalIdentity());
			setPartyName(transactionProcess.getPartyName());
			setCashiersCode(transactionProcess.getCashiersCode());
			setPayment(transactionProcess.getPayment());
			setDescription(transactionProcess.getDescription());
			setAttachedFiles(transactionProcess.getAttachedFiles());
			setPresentationId(transactionProcess.getPresentationId());
			
			setAnalystUserName(transactionProcess.getAnalystUserName());
			setAnalystFullName(transactionProcess.getAnalystFullName());
			setRequestType(transactionProcess.getRequestType());
			setRequestSubType(transactionProcess.getRequestSubType());
			setParcelDataList(transactionProcess.getParcelDataList());
			setAnalysisDictum(transactionProcess.getAnalysisDictum());
			setAdditionalObservations(transactionProcess.getAdditionalObservations());
			
			setDataEntryUserName(transactionProcess.getDataEntryUserName());
			setDataEntryFullName(transactionProcess.getDataEntryFullName());
			
			setApproverUserName(transactionProcess.getApproverUserName());
			setApproverFullName(transactionProcess.getApproverFullName());
		}
		
		initRequestTypes();
	}

	//Getters and setters for PRESENTATION activity--------------------------------
	public Date getPresentationDate() {
		return presentationDate;
	}
	public void setPresentationDate(Date presentationDate) {
		this.presentationDate = presentationDate;
	}

	public String getReceptionistFullName() {
		return receptionistFullName;
	}
	public void setReceptionistFullName(String receptionistFullName) {
		this.receptionistFullName = receptionistFullName;
	}

	public String getReceptionistUserName() {
		return receptionistUserName;
	}
	public void setReceptionistUserName(String receptionistUserName) {
		this.receptionistUserName = receptionistUserName;
	}

	public Long getPartyId() {
		return partyId;
	}
	public void setPartyId(Long partyId) {
		this.partyId = partyId;
	}
	
	public String getPartyName() {
		return partyName;
	}
	public void setPartyName(String partyName) {
		this.partyName = partyName;
	}

	public String getPartyNationalIdentity() {
		return partyNationalIdentity;
	}
	public void setPartyNationalIdentity(String partyNationalIdentity) {
		this.partyNationalIdentity = partyNationalIdentity;
	}
	
	public String getCashiersCode() {
		return cashiersCode;
	}
	public void setCashiersCode(String cashiersCode) {
		this.cashiersCode = cashiersCode;
	}
	
	public BigDecimal getPayment() {
		return payment;
	}
	public void setPayment(BigDecimal payment) {
		this.payment = payment;
	}
	
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}

	public List<AttachedFileData> getAttachedFiles() {
		return attachedFiles;
	}
	public void setAttachedFiles(List<AttachedFileData> attachedFiles) {
		this.attachedFiles = attachedFiles;
	}
	
	public Long getPresentationId() {
		return presentationId;
	}
	public void setPresentationId(Long presentationId) {
		this.presentationId = presentationId;
	}	
	//END Getters and setters for PRESENTATION activity--------------------------------


	//BEGIN getters and setters for ANALYSIS activity-------------------------------
	public HND_RequestType getRequestType() {
		return requestType;
	}
	public void setRequestType(HND_RequestType requestType) {
		this.requestType = requestType;
	}
	
	public String getAnalystUserName() {
		return analystUserName;
	}
	public void setAnalystUserName(String analystUserName) {
		this.analystUserName = analystUserName;
	}
	
	public String getAnalystFullName() {
		return analystFullName;
	}
	public void setAnalystFullName(String analystFullName) {
		this.analystFullName = analystFullName;
	}

	public HND_RequestSubType getRequestSubType() {
		return requestSubType;
	}
	public void setRequestSubType(HND_RequestSubType requestSubType) {
		this.requestSubType = requestSubType;
	}
	
	public List<ParcelData> getParcelDataList() {
		if (parcelDataList == null)
			parcelDataList = new ArrayList<ParcelData>();
		return parcelDataList;
	}
	public void setParcelDataList(List<ParcelData> parcelDataList) {
		this.parcelDataList = parcelDataList;
	}
	
	public String getAnalysisDictum() {
		return analysisDictum;
	}
	public void setAnalysisDictum(String analysisDictum) {
		this.analysisDictum = analysisDictum;
	}

	public String getAdditionalObservations() {
		return additionalObservations;
	}
	public void setAdditionalObservations(String additionalObservations) {
		this.additionalObservations = additionalObservations;
	}

	public boolean isRequestToProceed() {
		return requestToProceed;
	}
	public void setRequestToProceed(boolean requestToProceed) {
		if (requestToProceed)
			Transition.instance().setName("analysis_ok");
		else
			Transition.instance().setName("analysis_not_ok");
		this.requestToProceed = requestToProceed;
	}
	//END getters and setters for analysis activity-------------------------------
	
	//Variables for DATAENTRY activity------------------------------------
	public String getDataEntryUserName() {
		return dataEntryUserName;
	}
	public void setDataEntryUserName(String dataEntryUserName) {
		this.dataEntryUserName = dataEntryUserName;
	}

	public String getDataEntryFullName() {
		return dataEntryFullName;
	}
	public void setDataEntryFullName(String dataEntryFullName) {
		this.dataEntryFullName = dataEntryFullName;
	}	
	//END Variables for DATAENTRY activity--------------------------------
	
	//Variables for APPROVAL activity-------------------------------------
	public String getApproverUserName() {
		return approverUserName;
	}
	public void setApproverUserName(String approverUserName) {
		this.approverUserName = approverUserName;
	}

	public String getApproverFullName() {
		return approverFullName;
	}
	public void setApproverFullName(String approverFullName) {
		this.approverFullName = approverFullName;
	}
	//END Variables for APPROVAL activity---------------------------------


	public String getGid() {
		return gid;
	}
	public void setGid(String gid) {
		this.gid = gid;
	}

	public boolean getAttachInfo() {
		return attachInfo;
	}
	public void setAttachInfo(boolean attachInfo) {
		this.attachInfo = attachInfo;
	}
	
	public String getEditMode() {
		return editMode;
	}
	public void setEditMode(String editMode) {
		this.editMode = editMode;
	}

	public AttachedFileData getSelectedFile() {
		return selectedFile;
	}
	public void setSelectedFile(AttachedFileData selectedFile) {
		this.selectedFile = selectedFile;
	}
	
	public ParcelData getSelectedParcelData() {
		return selectedParcelData;
	}
	public void setSelectedParcelData(ParcelData selectedParcelData) {
		this.selectedParcelData = selectedParcelData;
	}

	public String deleteSelectedParcelData() {
		parcelDataList.remove(currentRow);
		return null;
	}
	
	
	public int getCurrentRow() {
		return currentRow;
	}
	public void setCurrentRow(int currentRow) {
		this.currentRow = currentRow;
	}
	
	public String getPropertyCode() {
		return propertyCode;
	}
	public void setPropertyCode(String propertyCode) {
		this.propertyCode = propertyCode;
	}

	public void newAttachedFile() {
		this.selectedFile = new AttachedFileData();
	}
	
	public void addParcelData() {
		//TODO: bring from database using propertyCode
		if (selectedParcelData != null) {
			//TODO: validar que no hayan repetidos
			getParcelDataList().add(selectedParcelData);
		}
	}
	
	public void acceptSelectedFile() {
		if ("0".equals(editMode)) { //agregando
			if (null == attachedFiles)
				attachedFiles = new ArrayList<AttachedFileData>();
			attachedFiles.add(selectedFile);
		}
		else if ("1".equals(editMode)) { //editando
			attachedFiles.set(currentRow, selectedFile);
		}
		
		setSelectedFile(null);
	}
	
	public void deleteSelectedFile() {
		attachedFiles.remove(currentRow);
	}
	
	
	public void attachFileListener(UploadEvent event) {
		UploadItem item = event.getUploadItem();
		if (null == attachedFiles)
			attachedFiles = new ArrayList<AttachedFileData>();
		
		AttachedFileData afd = new AttachedFileData();
		afd.setFileName(item.getFileName());
		afd.setFile(item.getFile());
		
		setSelectedFile(afd);
		
		item.getFile().deleteOnExit();
	}
	
	public String hasAttachedFiles() {
		if (attachInfo)
			return "si_adjunta";
		
		return "no_adjunta";
	}
	
	
	
	private ResourceBundle res = ResourceBundle.getBundle("messages", new Locale("es"));
	private List<SelectItem> requestTypes;
	private List<SelectItem> requestSubTypes;

	public void initRequestTypes() {
		requestTypes = new ArrayList<SelectItem>();
		HND_RequestType[] hndRequestTypes = HND_RequestType.values();
		requestTypes.add(new SelectItem("", "(seleccione clasificacion)"));
		for (HND_RequestType rt : hndRequestTypes)
			requestTypes.add(new SelectItem(rt, (String)res.getObject(rt.toString())));
	}
	
	public List<SelectItem> getRequestTypes() {
		return requestTypes;
	}
	public List<SelectItem> getRequestSubTypes() {
		List<HND_RequestMetaData> rmdList = HND_RequestMetaDataDAO.loadRequestMetaDataByRequestType(requestType);

		requestSubTypes = new ArrayList<SelectItem>();
		for (HND_RequestMetaData rmd : rmdList)
			requestSubTypes.add(new SelectItem(rmd.getName(), (String)res.getObject(rmd.getName().toString())));

		return requestSubTypes;
	}

	public TransactionProcessBean getTransactionProcess() {
		return transactionProcess;
	}

	public void dummy() {
		System.out.println("DUMMY CALL");
	}
}
